<template>
  <div class="hero-view">
    <BaseBox>
      <template #title>
        <BaseTitle>英雄百科</BaseTitle>
      </template>
      <template>
        <!-- 筛选盒子 -->
        <ul class="filter-box">
          <!-- 免费区域 -->
          <li class="free-box">
            <BaseTabs v-model="freeType" :tabs="freeTabs" />
          </li>
          <li>
            <!-- 定位区域 -->
            <div class="tab-box">
              <BaseTabs v-model="heroType" :tabs="typeTabs" />
            </div>
            <!-- 关键字检索 -->
            <div class="search-box">
              <!-- native 绑定给根标签 ，通过冒泡触发 -->
              <BaseInput
                placeholder="查询"
                v-model="q"
                @keyup.enter.native="query"
              />
            </div>
          </li>
        </ul>
        <!-- 列表 -->
        <ul class="list-wrap">
          <!-- 使用计算属性：filterHeros -->
          <li class="item" v-for="item in filterHeros" :key="item.heroId">
            <img :src="item.icon" alt="" />
            <p>{{ item.name }}</p>
          </li>
        </ul>
      </template>
    </BaseBox>
  </div>
</template>

<script>
import { getHeros } from "@/api/info";
export default {
  name: "HeroView",
  data() {
    return {
      freeType: "",
      freeTabs: [
        {
          value: "",
          text: "全部",
        },
        {
          value: "1",
          text: "本周免费",
        },
      ],
      heroType: "",
      typeTabs: [
        {
          value: "",
          text: "全部",
        },
        {
          value: "fighter",
          text: "战士",
        },
        {
          value: "mage",
          text: "法师",
        },
        {
          value: "assassin",
          text: "刺客",
        },
        {
          value: "tank",
          text: "坦克",
        },
        {
          value: "marksman",
          text: "射手",
        },
        {
          value: "support",
          text: "辅助",
        },
      ],
      // 英雄数组
      heroList: [],
      // 输入的内容
      q: "",
    };
  },
  async created() {
    const res = await getHeros();
    // console.log("res:", res);
    this.heroList = res.data.data;
  },
  methods: {
    async query() {
      // console.log("出发啦!");
      const res = await getHeros(this.q);
      this.heroList = res.data.data;
    },
  },
  computed: {  // 添加计算属性
    // 筛选之后的数据
    filterHeros() {
      // 返回筛选之后的结果 是否为本周免费
      // 免费条件：需要和 freeType 进行比较
      const _weekFilterRes = this.heroList.filter((v) => {
        // 基于 免费 和 全部进行筛选 如果 筛选为'' 是否免费都要
        if (this.freeType === "") {
          // 所有数据都要
          return true;
        }
        // 筛选条件不为空 只需要特定的数据
        if (v.isWeekFree === this.freeType) {
          return true;
        } else {
          return false;
        }
      });
      // 根据数据类型筛选
      return _weekFilterRes.filter(v => {
        if(this.heroType === ''){
          return true
        }
        // 判断是否roles 存在
        if(v.roles.includes(this.heroType)){
          // 需要该数据
          return true
        }else{
          // 排除
          return false
        }
      })
    },
  },
};
</script>

<style lang="less" scoped>
.hoverShadow () {
  transition: all 0.5s;
  cursor: pointer;
  &:hover {
    transform: translate3d(0, -3px, 0);
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.2);
  }
}
.hero-view {
  .filter-box {
    width: 80vw;
    margin-bottom: 10px;
    background-color: #f0f2f5;
  }
  .filter-box li {
    display: flex;
    align-items: center;
    height: 50px;
    .base-tab {
      margin-right: 10px;
    }
    &:before {
      content: "综合";
      color: white;
      display: inline-block;
      text-align: center;
      line-height: 40px;
      width: 80px;
      height: 100%;
      background-color: #2c88e2;
      margin-right: 10px;
    }
    &:first-child:before {
      border-top-left-radius: 10px;
    }

    &:last-child {
      &:before {
        border-bottom-left-radius: 10px;
      }
    }
  }
  .list-wrap {
    display: flex;
    flex-wrap: wrap;
    padding-left: 20px;
    .item {
      height: 120px;
      width: 120px;
      margin-bottom: 30px;
      padding: 10px 15px 0;
      .hoverShadow();
      img {
        display: block;
        border: 2px solid #2c88e2;
        width: 100%;
      }
      p {
        text-align: center;
        font-size: 13px;
      }
    }
  }
}
</style>
